//
//  BasePlayModel.m
//  PlayView
//
//  Created by houshijia on 16/9/25.
//  Copyright © 2016年 houshijia. All rights reserved.
//

#import "BasePlayModel.h"

@implementation BasePlayModel
- (id)initWithDic:(NSDictionary *)dic{
    
    if (self = [super init]) {
        
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
#pragma mark- 防闪退
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

#pragma mark UILabel自适应宽高
- (CGSize)sizeWithString:(NSString *)string Size:(CGSize)size Font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:size//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    
    return rect.size;
}

//时间戳转换
- (NSString *)since1970String:(NSString *)data
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd "];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[data intValue]];
    NSString * nowtimeStr = [formatter stringFromDate:date];
    
    return nowtimeStr;
}



/**
 *  获取当前最顶端的viewcontroller
 *
 *  @return viewcontroller
 */
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    if (!window) {
        NSLog(@"不存在KEYWINDOW!");
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
